package com.iwhalecloud.citybrain.flow.platform.manager.domain.model.factory;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.NextNodeParamEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNextNodeAssignee;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowNodeTempParamDomain;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNodeTempParamRepository;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

@Service
public class FlowNextNodeAssigneeFactory implements ApplicationContextAware {

    static FlowNodeTempParamRepository flowNodeTempParamRepository;

    public FlowNextNodeAssignee createFlowNextNodeAssignee(String processInstanceId, String nodeCode) {
        List<FlowNodeTempParamDomain> flowNodeTempParamDomains = flowNodeTempParamRepository.listNodeTempParam(
                processInstanceId, nodeCode, NextNodeParamEnum.NEXT_NODE_ASSIGNEE);
        FlowNextNodeAssignee flowNextNodeAssignee = null;
        if (flowNodeTempParamDomains != null && flowNodeTempParamDomains.size() > 0){
            FlowNodeTempParamDomain flowNodeTempParamDomain = flowNodeTempParamDomains.get(0);
            flowNextNodeAssignee = new FlowNextNodeAssignee();
            flowNextNodeAssignee.setProcessInstanceId(flowNodeTempParamDomain.getProcessInstanceId());
            flowNextNodeAssignee.setNodeCode(flowNodeTempParamDomain.getNodeCode());
            flowNextNodeAssignee.setNextAssignee(flowNodeTempParamDomain.getParamValue());
        }

        return flowNextNodeAssignee;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        flowNodeTempParamRepository = applicationContext.getBean(FlowNodeTempParamRepository.class);
    }
}